package com.dailyblue.java.spring.aop.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
@EnableAspectJAutoProxy  // 开启aop切面扫面，自动产生代理对象
public class SecondAdvice {

    @Pointcut(value = "execution(* com.dailyblue.java.spring.aop.target.impl.*.*(..))")
    private void execute() {
    }

    @Before("execute()")
    public void before() {
        System.out.println("我是注解写法，我会在目标对象执行之前执行");
    }

    @After("execute()")
    public void after() {
        System.out.println("我是注解写法，我会在之后执行！");
    }

    @AfterReturning("execute()")
    public void afterReturning(JoinPoint joinPoint) {
        System.out.println("我是注解写法，我会在目标对象执行之后执行");
        Object[] values = joinPoint.getArgs();
        System.out.println(Arrays.toString(values));
    }

    @Around("execute()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("环绕之前");
        // 执行目标对象的操作
        Object result = point.proceed();
        System.out.println("环绕之后");
        System.out.println("目标对象方法的返回结果：" + result);
        return result;
    }
}
